/***************************************************************************
 *   Copyright (C) 2009 by Zheng Yonglei
 *   zhengyonglei@gmail.com   *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/

#ifndef MYDICT_H
#define MYDICT_H

#include <QtGui/QWidget>
#include <QAction>
#include <QShortcut>
#include <QTreeWidgetItem>
#include <QScrollBar>
#include <QListWidgetItem>
#include <QMenu>
#include <QTimer>
#include <QProcess>
#include <QSound>
#include <QComboBox>
#include <QToolBar>

#include "ui_Dictionary.h"

#include "DictOptions.h"
#include "DictManager.h"
#include "DictSettingDialog.h"

class Dictionary : public QWidget
{
    Q_OBJECT

public:
    Dictionary(QWidget *parent = 0);
    ~Dictionary();

    void setInput(QString key);
    void setLookupDelayMS(int arg){DICT_OPT.mLookupDelayMS = arg;}

    void setDisplayFont(QFont& font);

public slots:
    void inputChanged(QString text);
    void itemSelectionChanged();

    void lookupTimeout();
	void lookupNormal();
	void lookupFuzzy();

	void addHistoryItem();

	void selectNormalLookup();
	void selectFuzzyLookup();

	void setShowWordList(bool);
	void setShowWordNav(bool);

    void showSettingDialog();

    void speak(QString key = QString());

    void itemActivated(QTreeWidgetItem* item, int column);

    void returnPressed();

    void insertIntroduction();

private:
    void insertHtml(QString html);
	void clearHtml();

	void setWordList(QStringList wordList);
	void lookupWord(QString key);

private:
    Ui::DictionaryClass ui;

    QString mLastQueryKey;

    float mArticleWidthRate;

    QTimer mTimer;

	QToolBar* mToolBar;
	QAction* mShowWordList;
	QAction* mShowWordNav;
	QComboBox* mSearchWordComboBox;
	QAction* mSelectNormalLookup;
	QAction* mSelectFuzzyLookup;
	QAction* mShowInstruction;
	QMenu* mLookupMenu;

	QAction* mLookupWordAction;
	QAction* settingAction;
	QAction* mSpeakAction;

	QString mCurKey;
	QVector<QTreeWidgetItem*> mNavItemListVector;
	map<QTreeWidgetItem*, int> mNavItemListMap;
	int mCurItem;
	QString mHtml;
};

#endif // MYDICT_H
